/// <license>
/// Copyright (c) 2002-2008, DotNetNuke Corporation
/// All rights reserved
///
/// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
/// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
/// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
/// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
///
/// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
/// of the Software.
///
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
/// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
/// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
/// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
/// DEALINGS IN THE SOFTWARE.
/// </license>

#region Using Statements

using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;

using DotNetNuke.Modules.Articles.Common;
using Constants = DotNetNuke.Modules.Articles.Common.Constants;
using DotNetNuke.Modules.Articles.Framework;

#endregion

namespace DotNetNuke.Modules.Articles.UI.Controls
{
    /// -----------------------------------------------------------------------------
    /// <summary>
    /// The SectionName skin object displays an section's Name.
    /// </summary>
    /// <history>
    /// 	[cnurse]	04/01/2006	created
    /// </history>
    /// -----------------------------------------------------------------------------
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal),
    AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal),
    ToolboxData("<{0}:SectionName runat=\"server\"> </{0}:SectionName>")]
    public class SectionName : ArticleSkinObjectBase
    {

        #region Private Members

        private string _DefaultKey;
        private string _DefaultText;

        private Style _EditButtonStyle;

        #endregion

        #region Protected Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets and sets the Default Css Class
        /// </summary>
        /// <history>
        ///     [cnurse]  03/18/2006  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected override String DefaultCssClass
        {
            get { return "Head"; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the design time text to display
        /// </summary>
        /// <history>
        ///     [cnurse]  04/07/2006  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected override string DesignTimeText
        {
            get { return Constants.TEXT_SectionName; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// Gets the text to display
        /// </summary>
        /// <history>
        ///     [cnurse]  04/07/2006  Created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Browsable(false)]
        protected override string DisplayText
        {
            get
            {
                //Get the Section Name Text
                string titleText = String.Empty;
                if (String.IsNullOrEmpty(DefaultKey) || DesignMode)
                    titleText = DefaultText;
                else
                    titleText = LocalizeString(DefaultKey);

                if (this.ParentArticlesModule != null && !this.ParentArticlesModule.IsAddMode 
                        && (Section != null) && (Section.Name != Null.NullString))
                {
                    titleText = Section.Name;
                }
                return titleText;
            }
        }

        #endregion

        #region Public Properties

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Default Text to use if the Section has no name (to be used if no resource key is specified)
        /// </summary>
        /// <history>
        /// 	[cnurse]	08/03/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Appearance"),
        DefaultValue(""),
        Description("The Default Text to use if the Section has no name.")]
        public string DefaultText
        {
            get { return _DefaultText; }
            set { _DefaultText = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Default Text to use if the Section has no name (to be used if no resource key is specified)
        /// </summary>
        /// <history>
        /// 	[cnurse]	08/03/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Bindable(true),
        Category("Localization"),
        DefaultValue(""),
        Description("The resource key to use to localize the default text.")]
        public string DefaultKey
        {
            get { return _DefaultKey; }
            set { _DefaultKey = value; }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// The Style to apply to the Edit Button
        /// </summary>
        /// <history>
        /// 	[cnurse]	08/07/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        [Category("Style"),
        DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
        PersistenceMode(PersistenceMode.InnerProperty),
        NotifyParentProperty(true),
        TypeConverter(typeof(ExpandableObjectConverter)),
        Description("The Style to apply to the Edit Button.")]
        public Style EditButtonStyle
        {
            get
            {
                if (_EditButtonStyle == null)
                    _EditButtonStyle = new Style();
                if (IsTrackingViewState)
                {
                    ((IStateManager)_EditButtonStyle).TrackViewState();
                }
                return _EditButtonStyle;
            }
        }

        #endregion

        #region Protected Methods

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// RenderContents renders the contents of the control
        /// </summary>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <history>
        /// 	[cnurse]	09/08/2007	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected override void RenderContents(HtmlTextWriter writer)
        {
            if (DesignMode)
            {
                writer.Write(DesignTimeText);
            }
            else
            {
                if (!String.IsNullOrEmpty(DefaultText))
                {
                    if (!IsAddMode)
                    {
                        RenderEditButton(writer);
                    }

                    writer.Write(DisplayText);
                }
            }
        }

        /// -----------------------------------------------------------------------------
        /// <summary>
        /// RenderEditButton renders the control's edit button
        /// </summary>
        /// <remarks>
        /// </remarks>
        /// <param name="writer">An HtmlTextWriter</param>
        /// <history>
        /// 	[cnurse]	08/07/2006	created
        /// </history>
        /// -----------------------------------------------------------------------------
        protected virtual void RenderEditButton(HtmlTextWriter writer)
        {
            bool showEdit = false;
            if (DesignMode)
                showEdit = true;
            else
                showEdit = CanEditSection && !IsEditMode;

            if (showEdit && Section != null)
            {
                if (EditButtonStyle != null)
                    EditButtonStyle.AddAttributesToRender(writer);

                //Render Hyperlink
                writer.AddAttribute(HtmlTextWriterAttribute.Href, ParentArticlesModule.ArticleURL(TabId, Null.NullInteger, Section.SectionId, Constants.KEY_EditSection));
                writer.AddAttribute(HtmlTextWriterAttribute.Title, Constants.TEXT_Edit);
                writer.RenderBeginTag(HtmlTextWriterTag.A);

                //Render Image
                writer.AddAttribute(HtmlTextWriterAttribute.Src, ResolveClientUrl(Constants.IMAGE_Edit));
                writer.AddAttribute(HtmlTextWriterAttribute.Border, "0");
                writer.RenderBeginTag(HtmlTextWriterTag.Img);

                //Render end of Image
                writer.RenderEndTag();

                //Render end of Hyperlink
                writer.RenderEndTag();
            }
        }

        #endregion

    }
}
